from abc import ABCMeta, abstractmethod



# 产品
# 构建者
# 指挥者
class Toy(metaclass=ABCMeta):
    """玩具"""
    def __init__(self, name):
        self._name = name
        self.__components = []

    def getName(self):
        return self._name

    def addComponent(self, component, count=1, unit='个'):
        self.__components.append([component, count, unit])

    @abstractmethod
    def feature(self):
        pass


class Car(Toy):
    """小车"""
    def feature(self):
        print("我是%s, 我可以快速奔跑······" % self._name)


class Manor(Toy):
    """庄园"""
    def feature(self):
        print("我是%s, 我可供观赏，也可用来游玩！" % self._name)


class ToyBuilder(metaclass=ABCMeta):
    """玩具构建者"""
    @abstractmethod
    def buildProduct(self):
        pass

class CarBuilder(ToyBuilder):
    def buildProduct(self):
        car = Car("迷你小车")
        car.addComponent("轮子", 4)
        car.addComponent("车身", 1)
        car.addComponent("发动机", 1)
        car.addComponent("方向盘")
        return car

class ManorBuilder(ToyBuilder):
    def buildProduct(self):
        manor = Manor("淘淘庄园")
        print("正在构建%s······" % manor.getName())
        return manor


class BuilderManager:
    """建构类的管理类"""
    def __init__(self):
        self.__carBuilder = CarBuilder()
        self.__manorBuilder = ManorBuilder()

    def buildCar(self, num):
        count = 0
        products = []
        while(count < num):
            car = self.__carBuilder.buildProduct()
            products.append(car)
            count += 1
            print("建造完成第%d辆%s" %(count, car.getName()))
        return products

    def buildManor(self, num):
        count = 0
        products = []
        while(count < num):
            manor = self.__manorBuilder.buildProduct()
            products.append(manor)
            count += 1
            print("建造完成第%d辆%s" %(count, manor.getName()))
        return products


if __name__ == "__main__":
    buildermgr = BuilderManager()
    buildermgr.buildManor(2)
    print()
    buildermgr.buildCar(4)

    